    //
//  BaseViewController.m
//  InspectionFor3M
//
//  Created by xugaoqiang on 11-7-25.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController()
{
    UIView * statusBack;
}

@property (retain) UIButton * versionButton;

@end

@implementation BaseViewController

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if(nibNameOrNil != nil)
    {
        NSString * iosStr = nil;
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            iosStr = nibNameOrNil;
        } else {
            iosStr = [NSString stringWithFormat:@"%@_iPad",nibNameOrNil];
        }
        self = [super initWithNibName:iosStr bundle:nibBundleOrNil];
    }
    
    if (self) {
        // Custom initialization.
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAllTimers) name:@"StopAllTimers" object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newVersionCome) name:@"NewVersionCome" object:nil];
    }
    return self;
}

-(id)init
{
    self = [super init];
    if(self)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAllTimers) name:@"StopAllTimers" object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newVersionCome) name:@"NewVersionCome" object:nil];
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self makeNavigationBar];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning 
{
    // Releases the view if it doesn't have a superview.

    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload 
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc 
{
    [(NSNotificationCenter *)[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)stopAllTimers
{

}

-(void)newVersionCome
{
    self.versionButton.hidden = ![GlobalInfo sharedInstance].isVersionNew;
}

#pragma mark - navigation methods
-(void)makeNavigationBar
{
    if([[self navigationBarTitle] length] > 0)
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 120, 44)];
        titleLabel.font = [UIFont boldSystemFontOfSize:16];
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = UITextAlignmentCenter;
        titleLabel.text = [self navigationBarTitle];
        self.navigationItem.titleView = titleLabel;
    }
    
    if([self isNavigationBackEnabled] == YES)
    {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [backButton setBackgroundImage:[UIImage imageNamed:@"btn_top2.png"]
                              forState:UIControlStateNormal];
        [backButton setBackgroundImage:[UIImage imageNamed:@"btn_top2_selected.png"]
                              forState:UIControlStateHighlighted];
        [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        backButton.frame = CGRectMake(0, 0, 55, 30);
        
        UIBarButtonItem* leftBtnItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = leftBtnItem;
    }
    
    if([self isNewVersionButtonEnabled] == YES)
    {
        _versionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_versionButton setBackgroundImage:[UIImage imageNamed:@"newbutton_normal"]
                                  forState:UIControlStateNormal];
        [_versionButton setBackgroundImage:[UIImage imageNamed:@"newbutton_press"]
                                  forState:UIControlStateHighlighted];
        [_versionButton addTarget:self action:@selector(newVersionButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        _versionButton.frame = CGRectMake(0, 0, 73, 36);
        
        UIBarButtonItem* rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:_versionButton];
        self.navigationItem.rightBarButtonItem = rightBtnItem;
        
        self.versionButton.hidden = ![GlobalInfo sharedInstance].isVersionNew;
    }
}

-(void)clearNavigationBar
{
    self.navigationItem.rightBarButtonItem = nil;
    _versionButton = nil;
}

-(NSString *)navigationBarTitle
{
    return @"";
}


-(BOOL)isNavigationBackEnabled
{
    return YES;
}

-(BOOL)isNewVersionButtonEnabled
{
    return NO;
}

-(IBAction)backButtonPressed:(id)sender
{
    [[GlobalService sharedInstance] removeHandler:self];
    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)newVersionButtonPressed
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ShowUpdateAlert" object:nil];
}


@end
